using System;
using System.Collections.Specialized;
using System.Text;
using My.SharePoint.STSADM.Commands.OperationHelpers;
using My.SharePoint.STSADM.Commands.SPValidators;
using Microsoft.SharePoint;

namespace My.SharePoint.STSADM.Commands.MyCommands
{
    public class MyCommand : SPOperation
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MyCommand"/> class.
        /// </summary>
        public MyCommand()
        {
            SPParamCollection parameters = new SPParamCollection();
            parameters.Add(new SPParam("url", "url", true, null, new SPUrlValidator(), "Please specify the site collection."));
            
            StringBuilder sb = new StringBuilder();
            sb.Append("\r\n\r\nMy custom command.\r\n\r\nParameters:");
            sb.Append("\r\n\t-url <web url>");
            Init(parameters, sb.ToString());
        }

        /// <summary>
        /// Gets the help message.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <returns></returns>
        public override string GetHelpMessage(string command)
        {
            return HelpMessage;
        }

        /// <summary>
        /// Runs the specified command.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <param name="keyValues">The key values.</param>
        /// <param name="output">The output.</param>
        /// <returns></returns>
        public override int Execute(string command, StringDictionary keyValues, out string output)
        {
            output = string.Empty;

            string url = Params["url"].Value;
            if (url != null)
                url = url.TrimEnd('/');

            using (SPSite site = new SPSite(url))
            using (SPWeb web = site.AllWebs[Utilities.GetServerRelUrlFromFullUrl(url)])
            {
                Log("The following web was provided: {0}", web.Name);
            }

            // Use the ErrorCodes enum to return error codes: 
            // ErrorCodes.SyntaxError
            // ErrorCodes.GeneralError

            // Return OUTPUT_SUCCESS for success.
            return OUTPUT_SUCCESS;
        }

        
    }
}
